package com.parkreport.admin.carcolor.service;

import java.util.List;

import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.parkreport.vo.CarcolorVO;

public class CarcolorService {
	public List find(CarcolorVO vo,PageHelper pager){
		StringBuilder sql = new StringBuilder("select * from CAR_COLOR where 1=1 and DEL_FLAG='0'");
		List<CarcolorVO> list = DB4ObjUtil.executePager4Obj(new CarcolorVO(), sql.toString(),pager);
		
		return list;
	}
	
	public CarcolorVO get(String id){
		String sql = "select * from CAR_COLOR where 1=1 and ID=?";
		List<CarcolorVO> list = DB4ObjUtil.execute4Obj(new CarcolorVO(), sql, id);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	

	public void delete(String ids){
		//String sql = "delete from CAR_COLOR where id=?";
		String sql = "update CAR_COLOR set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			
		}
	}
	
	public void delete(String[] ids){
		//String sql = "delete from CAR_COLOR where id=?";
		String sql = "update CAR_COLOR set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
			
		}
		
		
	}
	public String save(CarcolorVO vo){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				if(StringUtil.isBlank(vo.getId())){
						String id = StringUtil.getUuid();
						String sql = "insert into CAR_COLOR(ID,COLOR_CODE,COLOR_NAME,PRE_PERSON,DEL_FLAG) values(?,?,?,?,?)";
						DBBaseUtil.executeUpdate(sql, id,vo.getColorCode(),vo.getColorName(),vo.getPrePerson(),"0");
				}else{
					
					String sql = "update CAR_COLOR set COLOR_CODE=?,COLOR_NAME=?,PRE_PERSON=?  where id=?";
					DBBaseUtil.executeUpdate(sql, vo.getColorCode(),vo.getColorName(),vo.getPrePerson(),vo.getId());
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public String isApply(String username,String id){
		String sql = "select * from CAR_COLOR where 1=1 and COLOR_CODE=? and ID<>?";
		List<CarcolorVO> list = DB4ObjUtil.execute4Obj(new CarcolorVO(), sql, username,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(CarcolorVO vo){
		String sql = "select * from CAR_COLOR where 1=1 and COLOR_CODE=? and ID<>?";
		List<CarcolorVO> list = DB4ObjUtil.execute4Obj(new CarcolorVO(), sql, vo.getColorCode(),vo.getId());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
}
